package com.enjoy.book.biz;

import com.enjoy.book.bean.Member;

import com.enjoy.book.dao.MemberData;


import java.sql.SQLException;
import java.util.List;

/**
 * @author 赵洋
 */
public class MemberBiz {
    MemberData memberData = new MemberData();


    public int add(String name,String pwd,double balance,String tel,String idNumber){
        int count = 0;
        try {
            count = memberData.add(name,pwd,balance,tel,idNumber);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }
    public int add(Member member){
        return add(member.getName(),member.getPwd(),member.getBalance(),member.getTel(),member.getIdNumber());
    }
    public int modify(Member member){
        return modify(member.getId(),member.getName(),member.getPwd(),member.getBalance(),member.getTel(),member.getIdNumber());
    }
    public int modify(long id,String name,String pwd,double balance,String tel,String idNumber){
        int count = 0;
        try {
            count = memberData.modify(id,name,pwd,balance,tel,idNumber);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }
    public int remove(long id) throws Exception {
        //1.判断会员账号余额 >0 :提示不能删除
        Member member = getById(id);
        if(member.getBalance()>0){
            throw new Exception("此会员消费金额大于0,删除失败");
        }
        //2.有外键不能删除
        if(memberData.exits(id)){
            throw new Exception("此会员有子信息,删除失败");
        }
        //3.删除
        int count =0;
        try {
            count = memberData.remove(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;

    }
    public int modifyBalance(String idNumber,double amount){
        int count = 0;
        try {
            count = memberData.modifyBalance(idNumber,amount);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }
     public List<Member> getAll(){

        List<Member> members = null;
        try {
            members =  memberData.getAll();

        } catch (SQLException throwables) {
            throwables.printStackTrace();

        }
        return members;

    }
    public Member getById(long id){
        Member member = null;
        try {
            member = memberData.getById(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return member;
    }


    /**
     * 会员与会员类型
     * @param idNumber
     * @return
     */
    public Member getByIdNumber(String idNumber){
        Member member = null;
        try {
            member = memberData.getByIdNumber(idNumber);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return member;
    }

}
